package demo4;

import java.util.concurrent.TimeUnit;

public class Main1 {
    public static void main(String[] args) {
        //创建一个交易场所，即是创建一个阻塞队列
        MyBlockingQueue queue=new MyBlockingQueue(100);
        //创建一个生产者线程
        Thread producer =new Thread(()->{
            Integer num=0;
            while (true) {
                try {
                    queue.put(num);
                    System.out.println("生产了："+num);
                    num++;
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
        //创建一个消费者线程
        Thread consumer =new Thread(()->{
            while (true){
                try {
                    Integer value=queue.take();
                    System.out.println("消费了： "+value);
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        consumer.start();
    }
}
